<?php
class ClientResponseHandler  {
	
	var $key;
	
	var $parameters;
	
	var $debugInfo;
	
	var $content;
	
	function __construct() {
		$this->ClientResponseHandler();
	}
	
	function ClientResponseHandler() {
		$this->key = "";
		$this->parameters = array();
		$this->debugInfo = "";
		$this->content = "";
	}
		
	/**
	 */
	function getKey() {
		return $this->key;
	}
	
	/**
	 */
	function setKey($key) {
		$this->key = $key;
	}
	
	/**
	 */
	function setContent($content) {
		$this->content = $content;
		
		$xml = simplexml_load_string($this->content);
		$encode = $this->getXmlEncode($this->content);
		
		if($xml && $xml->children()) {
			foreach ($xml->children() as $node){
				//���ӽڵ�
				if($node->children()) {
					$k = $node->getName();
					$nodeXml = $node->asXML();
					$v = substr($nodeXml, strlen($k)+2, strlen($nodeXml)-2*strlen($k)-5);
					
				} else {
					$k = $node->getName();
					$v = (string)$node;
				}
				
				if($encode!="" && $encode != "UTF-8") {
					$k = mb_convert_encoding($k,"utf-8",$encode);
					$v = mb_convert_encoding($v,"utf-8",$encode);
					//$k = iconv("UTF-8", $encode, $k);
					//$v = iconv("UTF-8", $encode, $v);
				}
				
				$this->setParameter($k, $v);			
			}
		}	
	}
	
	/**
	 */
	function setContent_backup($content) {
		$this->content = $content;
		$encode = $this->getXmlEncode($this->content);
		$xml = new SofeeXmlParser(); 
		$xml->parseFile($this->content); 
		$tree = $xml->getTree(); 
		unset($xml); 
		foreach ($tree['root'] as $key => $value) {
			if($encode!="" && $encode != "UTF-8") {
				$k = mb_convert_encoding($key, $encode, "UTF-8");
				$v = mb_convert_encoding($value[value], $encode, "UTF-8");								
			}
			else 
			{
				$k = $key;
				$v = $value[value];
			}
			$this->setParameter($k, $v);
		}
	}
	
	
	
	/**
	 */	
	function getContent() {
		return $this->content;
	}
	
	/**
	 */	
	function getParameter($parameter) {
		return $this->parameters[$parameter];
	}
	
	/**
	 */		
	function setParameter($parameter, $parameterValue) {
		$this->parameters[$parameter] = $parameterValue;
	}
	
	/**
	 */	
	function getAllParameters() {
		return $this->parameters;
	}	
	
	/**
	 */	
	function isTenpaySign() {
		
		//F("tenpay2_1_param",$this->parameters);
		$signPars = "";
		$param    = $this->parameters;
		$filters  = array("_visitid","_URL_","cateid","orderno","sign");//
		ksort($param);
		foreach($param as $k => $v) {
			if( "" != $v && !in_array($k,$filters)) {
				$signPars  .= $k . "=" . $v . "&";
				//$config[$k] = $v ;
			}
		}
		
		//$config['key'] = $this->getKey();
		//F("tenpay3_2_config",$config);
		//F("tenpay2_2_config",$config);
		$signPars .= "key=" . $this->getKey();
		//F("tenpay2_3_key",$signPars);
		
		$sign = strtolower(md5($signPars));
		$tenpaySign = strtolower($this->getParameter("sign"));
				
		$this->_setDebugInfo($signPars . " => sign:" . $sign ." tenpaySign:" . $this->getParameter("sign"));
		//F("tenpay2_4_sign",array("c"=>$sign,"t"=>$tenpaySign));

		return $sign == $tenpaySign;
	}
	
	/**
	 */		
	function getDebugInfo() {
		return $this->debugInfo;
	}
	
	/**
	 */	
	function getXmlEncode($xml) {
		$ret = preg_match ("/<?xml[^>]* encoding=\"(.*)\"[^>]* ?>/i", $xml, $arr);
		if($ret) {
			return strtoupper ( $arr[1] );
		} else {
			return "";
		}
	}
	
	/**
	 */	
	function _setDebugInfo($debugInfo) {
		$this->debugInfo = $debugInfo;
	}
	
	/**
	 */	
	function _isTenpaySign($signParameterArray) {
			
		$signPars = "";
		$param    = $this->parameters;
		$filters  = array("_visitid","_URL_","cateid","orderno","sign");
		ksort($param);
		foreach($param as $k => $v) {
			if( "" != $v && !in_array($k,$filters)) {
				$signPars  .= $k . "=" . $v . "&";
				$config[$k] = $v ;
			}
		}
		
		$signPars .= "key=" . $this->getKey();
		$sign = strtolower(md5($signPars));
		$tenpaySign = strtolower($this->getParameter("sign"));	
		$this->_setDebugInfo($signPars . " => sign:" . $sign .
				" tenpaySign:" . $this->getParameter("sign"));
		
		return $sign == $tenpaySign;		
		
	}
	
}


?>